package cn.wolfcode.controller;

import cn.wolfcode.entity.Destination;
import cn.wolfcode.entity.Strategy;
import cn.wolfcode.entity.Travel;
import cn.wolfcode.entity.Userinfo;
import cn.wolfcode.es.domain.DestinationES;
import cn.wolfcode.es.domain.StrategyES;
import cn.wolfcode.es.domain.TravelES;
import cn.wolfcode.es.domain.UserInfoES;
import cn.wolfcode.es.query.SearchQuery;
import cn.wolfcode.es.service.ISearchService;
import cn.wolfcode.service.IDestinationService;
import cn.wolfcode.service.IStrategyService;
import cn.wolfcode.service.ITravelService;
import cn.wolfcode.service.IUserinfoService;
import cn.wolfcode.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;

@RestController
@CrossOrigin
public class SearchController {
    @Autowired
    private ISearchService searchService;

    @Autowired
    private IUserinfoService userinfoService;

    @Autowired
    private ITravelService travelService;

    @Autowired
    IStrategyService strategyService;

    @Autowired
    IDestinationService destinationService;

    @GetMapping("/q")
    public JSONResult search(SearchQuery qo) throws UnsupportedEncodingException {
        // 解码中文
        qo.setKeyword(URLDecoder.decode(qo.getKeyword(), StandardCharsets.UTF_8));
        HashMap<String, Object> map = new HashMap<>();
        switch (qo.getType()) {
            case -1: {
                Page<Strategy> strategies = searchService.searchWithHighlight(StrategyES.INDEX_NAME, StrategyES.TYPE_NAME, qo, Strategy.class, "title", "subtitle", "summary");
                Page<Travel> travels = searchService.searchWithHighlight(TravelES.INDEX_NAME, TravelES.TYPE_NAME, qo, Travel.class, "title", "summary");
                Page<Userinfo> userinfos = searchService.searchWithHighlight(UserInfoES.INDEX_NAME, UserInfoES.TYPE_NAME, qo, Userinfo.class, "city", "info", "nickname");
                Page<Destination> destinations = searchService.searchWithHighlight(DestinationES.INDEX_NAME, DestinationES.TYPE_NAME, qo, Destination.class, "name", "info");
                List<Strategy> content1 = strategies.getContent();
                List<Travel> content2 = travels.getContent();
                List<Userinfo> content3 = userinfos.getContent();
                List<Destination> content4 = destinations.getContent();
                for (Travel travel : content2) {
                    Userinfo userinfo = userinfoService.getById(travel.getAuthorId());
                    travel.setAuthor(userinfo);
                }
                int total = content1.size() + content2.size() + content3.size() + content4.size();
                HashMap<String, Object> innerMap = new HashMap<>();
                innerMap.put("strategys", content1);
                innerMap.put("travels", content2);
                innerMap.put("users", content3);
                innerMap.put("dests", content4);
                innerMap.put("total", total);
                map.put("result", innerMap);
                map.put("qo", qo);
                break;
            }
            case 1: {
                Page<Strategy> page = searchService.searchWithHighlight(StrategyES.INDEX_NAME, StrategyES.TYPE_NAME, qo, Strategy.class, "title", "subTitle", "summary");
                map.put("page", page);
                map.put("qo", qo);
                break;
            }
            case 2: {
                Page<Travel> page = searchService.searchWithHighlight(TravelES.INDEX_NAME, TravelES.TYPE_NAME, qo, Travel.class, "title", "summary");
                map.put("page", page);
                map.put("qo", qo);
                break;
            }
            case 3: {
                Page<Userinfo> page = searchService.searchWithHighlight(UserInfoES.INDEX_NAME, UserInfoES.TYPE_NAME, qo, Userinfo.class, "info", "city", "nickname");
                map.put("page", page);
                map.put("qo", qo);
                break;
            }
            case 0: {
                Destination dest = destinationService.queryByName(qo.getKeyword());
                HashMap<String, Object> innerMap = new HashMap<>();
                if (dest != null) {
                    List<Strategy> strategies = strategyService.queryByDestId(dest.getId());
                    List<Travel> travels = travelService.queryByDestId(dest.getId());
                    for (Travel travel : travels) {
                        Userinfo userinfo = userinfoService.getById(travel.getAuthorId());
                        travel.setAuthor(userinfo);
                    }
                    List<Userinfo> users = userinfoService.queryByCity(dest.getName());
                    innerMap.put("strategys", strategies);
                    innerMap.put("travels", travels);
                    innerMap.put("users", users);
                    innerMap.put("qo", qo);
                }
                map.put("result", innerMap);
                map.put("qo", qo);
                map.put("dest", dest);
                break;
            }

        }
        return JSONResult.success(map);
    }

}
